//
//  WHTStroryVC.m
//  WZLProduct
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "WHTStroryVC.h"
#import "firstPageFirstSecctionCell_Ht.h"
#import "WHTFirstPageStoryModel.h"
@interface WHTStroryVC ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource; /**< 数据源 */
@property (nonatomic, assign) NSInteger nextPageId;
@end


@implementation WHTStroryVC
static NSString * WHTStrotyCollectionView_cell_id = @"WHTStrotyCollectionView_cell_id";



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.nextPageId = 0;
        self.dataSource = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.91 green:0.87 blue:0.78 alpha:1];
    [self setNavigationBar];
    [self creatCollectionView];
    [self MMHUDBalloon];
    [self dataHandel];
    [self addFooter];
}



#pragma mark- 设置导航栏
- (void)setNavigationBar{
    self.navigationItem.title = @"精选故事";
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"HT_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back:)]];
}
#pragma mark 返回
- (void)back:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark- 集合视图
- (void)creatCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.91 green:0.87 blue:0.78 alpha:1];
    layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 30) / 2, ([UIScreen mainScreen].bounds.size.width - 30) / 2 + 30);

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    //注册
    [self.collectionView registerClass:[firstPageFirstSecctionCell_Ht class] forCellWithReuseIdentifier:WHTStrotyCollectionView_cell_id];
}
#pragma mark 集合视图代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    firstPageFirstSecctionCell_Ht *cell = [collectionView dequeueReusableCellWithReuseIdentifier:WHTStrotyCollectionView_cell_id forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}


#pragma mark- 处理数据
- (void)dataHandel{

    NSString *DataStr = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld",self.nextPageId];
    
    [RequestData creatingDataTask:DataStr parameters:nil Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        [self setData:resuposeObject];
    }];
       [MMProgressHUD dismissWithSuccess:@"精选故事!"];
}

- (void)setData:(id)data{
    for (NSDictionary *dic in data[@"data"][@"hot_spot_list"]) {
        WHTFirstPageStoryModel *model = [[WHTFirstPageStoryModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataSource addObject:model];
        
    }
    [self.collectionView reloadData];
}

#pragma mark 上拉加载更多
- (void)addFooter
{
    __weak WHTStroryVC *tempVC = self;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.nextPageId += 12;
        [tempVC dataHandel];
        [tempVC.collectionView.mj_footer endRefreshing];
    }];
    
    [footer setTitle:@"BAT_Click or drag up to refresh" forState:MJRefreshStateIdle];
    [footer setTitle:@"BAT_Loading more ..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"BAT_No more data" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.textColor = kAppearanceColor;
    self.collectionView.mj_footer = footer;
    
}



@end
